package com.platform.core.web.controller;

import cn.hutool.core.collection.CollUtil;
import com.platform.comm.web.results.PageListDto;
import com.platform.comm.web.results.PageListJsonResult;
import com.platform.core.service.SysLogService;
import com.platform.dto.LogQo;
import com.querydsl.core.BooleanBuilder;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Description: 日志管理
 *
 * @author libin Created on 2019/7/19
 **/
@RestController
@RequestMapping("management/log")
public class SysLogController extends BaseController {
    private final SysLogService sysLogService;

    public SysLogController(SysLogService sysLogService) {
        this.sysLogService = sysLogService;
    }

    @PostMapping(name = "分页查询", path = "search")
    public PageListJsonResult<LogQo> search(@RequestBody Map<String, String> paramsMap) {
        MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
        if (CollUtil.isNotEmpty(paramsMap)) {
            paramsMap.forEach(parameters::add);
        }
        return queryByPaging(sysLogService::queryLogs, new BooleanBuilder(), getPageable(paramsMap), parameters);
    }

    @PostMapping(name = "分页查询", path = "/queryGatherOptLogs")
    public PageListDto<LogQo> queryGatherOptLogs(@RequestBody Map<String, String> parameters) {
        return PageListDto.of(sysLogService.queryGatherOptLogs(new BooleanBuilder(), getPageable(parameters), parameters));
    }

    @PostMapping(name = "查询日志", path = "/queryGatherOptLogById")
    public LogQo queryGatherOptLogById(@RequestBody Map<String, String> parameters) {
        return sysLogService.queryLogById(parameters.get("id"));
    }

    @PostMapping(name = "日志创建", path = "/create")
    public void create(@RequestBody LogQo entity) {
        sysLogService.create(entity);
    }
}
